//
//  SimonViewController.m
//  Simon
//
//  Created by Mr. Steptoe on 5/1/10.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import "SimonViewController.h"

@implementation SimonViewController

@synthesize redButton, yellowButton, greenButton, blueButton, startSequenceButton, sequenceFlasher;

/**
 PLAYER SECTION
 
 After the sequence has run, it is up to the player to respond by tapping in the
 correct sequence of colors. These are methods concerned with interpreting player
 input and recognizing a win/loss condition.
 
 **/


-(IBAction)redButtonPressed:(id)sender{
	printf("Red button pressed!\n");
	if([self checkInput:0]){
		currentNumber++;
		
		if(currentNumber == MAXSEQUENCE){
			[self win];
		}
	} else {
		[self lose];
	}
}

-(IBAction)yellowButtonPressed:(id)sender{
	printf("Yellow button pressed!\n");	
	if([self checkInput:1]){
		currentNumber++;
		
		if(currentNumber == MAXSEQUENCE){
			[self win];
		}
	} else {
		[self lose];
	}
}

-(IBAction)greenButtonPressed:(id)sender{
	printf("Green button pressed!\n");	
	if([self checkInput:2]){
		currentNumber++;
		
		if(currentNumber == MAXSEQUENCE){
			[self win];
		}
	} else {
		[self lose];
	}
}

-(IBAction)blueButtonPressed:(id)sender{
	printf("Blue button pressed!\n");
	if([self checkInput:3]){
		currentNumber++;
		
		if(currentNumber == MAXSEQUENCE){
			[self win];
		}
	} else {
		[self lose];
	}
}

-(BOOL)checkInput:(int)color{
	if(color == sequenceArray[currentNumber]){
		printf("Correct entry\n");
		return TRUE;
	} else {
		printf("Incorrect entry\n");
		return FALSE;
	}
}

-(void)lose{
	printf("Wrong input, you lose!\n");
	//shut off player input
	redButton.enabled = NO;
	yellowButton.enabled = NO;
	greenButton.enabled = NO;
	blueButton.enabled = NO;
	
	sequenceCounter = 0;
	currentNumber = 0;
	//make new seqence for next game
	[self buildSequenceArray];
}

-(void)win{
	printf("You got them all, you win!\n");
	//shut off player input
	redButton.enabled = NO;
	yellowButton.enabled = NO;
	greenButton.enabled = NO;
	blueButton.enabled = NO;
	
	sequenceCounter = 0;
	currentNumber = 0;
	//make new seqence for next game
	[self buildSequenceArray];
}

/**
 SEQUENCE SECTION
 
 When the player taps "Start Sequence" the randomly-generated sequence flashes by. 
 These are methods concerned with setting up and displaying the sequence.
 
 **/

-(IBAction)startSequenceButtonPressed:(id)sender{
	printf("Starting sequence...\n");
	[self setUpTimer];
	// shut off input to startSequence
	startSequenceButton.enabled = NO;
}

//Sets up the timer that will count down the sequence of colors
-(void)setUpTimer {
	printf("Setting up the timer...\n");
	
	sequencer=[NSTimer scheduledTimerWithTimeInterval:0.7f
												target:self
											  selector:@selector(sequence:)
											  userInfo:nil
											   repeats:YES];
}

//Timer operation
-(void)sequence:(NSTimer *)theTimer{
	
	//For more complex game...
	/** 
	if (sequenceCounter == currentNumber){
		[self stopTimer];
	} else {
		printf("%d \n", sequenceCounter);
		sequenceCounter = sequenceCounter + 1;
	}
	**/
	
	//For a constant, limited sequence
	if(currentNumber < MAXSEQUENCE){
	
	switch (sequenceArray[currentNumber]) {
		case 0:
			sequenceFlasher.image = [UIImage imageNamed:@"color-red.jpg"];
			printf("Red flashed...\n");
			break;
		case 1:
			sequenceFlasher.image = [UIImage imageNamed:@"color-yellow.jpg"];
			printf("Yellow flashed...\n");
			break;
		case 2:
			sequenceFlasher.image = [UIImage imageNamed:@"color-green.jpg"];
			printf("Green flashed...\n");
			break;
		case 3:
			sequenceFlasher.image = [UIImage imageNamed:@"color-blue.jpg"];
			printf("Blue flashed...\n");
			break;
		default:
			break;
	}
	
	currentNumber++;
	
	} else {
		sequenceFlasher.image = nil;
		[self stopTimer];
	}
		
}

//Stops the timer
-(void)stopTimer{
	printf("Stopping timer...");
	if (sequencer != nil) {
		[sequencer invalidate];
		sequencer = nil;
	}
	//resets the cursor at the first element in the sequenceArray, ready for player input.
	currentNumber = 0;
	
	redButton.enabled = YES;
	yellowButton.enabled = YES;
	greenButton.enabled = YES;
	blueButton.enabled = YES;
}

/**
 MISC SECTION
 
 **/

//Adds a new color into the color sequence on the end
-(void)addElement{
	sequenceArray[sequenceCounter-1] = arc4random() % 4;
}

//Prints out the entire sequence in numerical form (debugging purposes)
-(void)printSequenceArray{
	for(int i = 0; i<MAXSEQUENCE; i++){
		printf("%d ", sequenceArray[i]);
	}
	printf("\n");
}

-(void)buildSequenceArray{
	for(int i = 0; i<MAXSEQUENCE; i++){
		sequenceArray[i] = arc4random() % 4;
	}
	[self printSequenceArray];
	startSequenceButton.enabled = YES;
}

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

- (void)viewDidLoad {
	printf("View loaded...\n");
	
	gameRunning = TRUE;
	
	sequenceCounter = 0;
	currentNumber = 0;
	[self buildSequenceArray];
	
	// shut off input to buttons (because the player doesnt know the sequence yet).
	
    [super viewDidLoad];
	
	self.redButton.enabled = NO;
	self.yellowButton.enabled = NO;
	self.greenButton.enabled = NO;
	self.blueButton.enabled = NO;
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end
